package com.google.android.vending.expansion.downloader.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDoneException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;
import android.util.Log;

public class DownloadsDB
{
  private static final int CONTROL_IDX = 7;
  private static final int CURRENTBYTES_IDX = 4;
  private static final String DATABASE_NAME = "DownloadsDB";
  private static final int DATABASE_VERSION = 7;
  private static final String[] DC_PROJECTION = { "FN", "URI", "ETAG", "TOTALBYTES", "CURRENTBYTES", "LASTMOD", "STATUS", "CONTROL", "FAILCOUNT", "RETRYAFTER", "REDIRECTCOUNT", "FILEIDX" };
  private static final int ETAG_IDX = 2;
  private static final int FILENAME_IDX = 0;
  private static final int INDEX_IDX = 11;
  private static final int LASTMOD_IDX = 5;
  public static final String LOG_TAG = DownloadsDB.class.getName();
  private static final int NUM_FAILED_IDX = 8;
  private static final int REDIRECT_COUNT_IDX = 10;
  private static final int RETRY_AFTER_IDX = 9;
  private static final int STATUS_IDX = 6;
  private static final int TOTALBYTES_IDX = 3;
  private static final int URI_IDX = 1;
  private static DownloadsDB mDownloadsDB;
  int mFlags;
  SQLiteStatement mGetDownloadByIndex;
  final SQLiteOpenHelper mHelper;
  long mMetadataRowID = -1L;
  int mStatus = -1;
  SQLiteStatement mUpdateCurrentBytes;
  int mVersionCode = -1;

  private DownloadsDB(Context paramContext)
  {
    this.mHelper = new DownloadsContentDBHelper(paramContext);
    Cursor localCursor = this.mHelper.getReadableDatabase().rawQuery("SELECT APKVERSION,_id,DOWNLOADSTATUS,DOWNLOADFLAGS FROM MetadataColumns LIMIT 1", null);
    if ((localCursor != null) && (localCursor.moveToFirst()))
    {
      this.mVersionCode = localCursor.getInt(0);
      this.mMetadataRowID = localCursor.getLong(1);
      this.mStatus = localCursor.getInt(2);
      this.mFlags = localCursor.getInt(3);
      localCursor.close();
    }
    mDownloadsDB = this;
  }

  public static DownloadsDB getDB(Context paramContext)
  {
    try
    {
      if (mDownloadsDB == null);
      for (DownloadsDB localDownloadsDB = new DownloadsDB(paramContext); ; localDownloadsDB = mDownloadsDB)
        return localDownloadsDB;
    }
    finally
    {
    }
  }

  private SQLiteStatement getDownloadByIndexStatement()
  {
    if (this.mGetDownloadByIndex == null)
      this.mGetDownloadByIndex = this.mHelper.getReadableDatabase().compileStatement("SELECT _id FROM DownloadColumns WHERE FILEIDX = ?");
    return this.mGetDownloadByIndex;
  }

  private SQLiteStatement getUpdateCurrentBytesStatement()
  {
    if (this.mUpdateCurrentBytes == null)
      this.mUpdateCurrentBytes = this.mHelper.getReadableDatabase().compileStatement("UPDATE DownloadColumns SET CURRENTBYTES = ? WHERE FILEIDX = ?");
    return this.mUpdateCurrentBytes;
  }

  public void close()
  {
    this.mHelper.close();
  }

  protected DownloadInfo getDownloadInfoByFileName(String paramString)
  {
    SQLiteDatabase localSQLiteDatabase = this.mHelper.getReadableDatabase();
    Cursor localCursor = null;
    try
    {
      localCursor = localSQLiteDatabase.query("DownloadColumns", DC_PROJECTION, "FN = ?", new String[] { paramString }, null, null, null);
      if ((localCursor != null) && (localCursor.moveToFirst()))
      {
        DownloadInfo localDownloadInfo = getDownloadInfoFromCursor(localCursor);
        return localDownloadInfo;
      }
      return null;
    }
    finally
    {
      if (localCursor != null)
        localCursor.close();
    }
  }

  public DownloadInfo getDownloadInfoFromCursor(Cursor paramCursor)
  {
    DownloadInfo localDownloadInfo = new DownloadInfo(paramCursor.getInt(11), paramCursor.getString(0), getClass().getPackage().getName());
    setDownloadInfoFromCursor(localDownloadInfo, paramCursor);
    return localDownloadInfo;
  }

  public DownloadInfo[] getDownloads()
  {
    SQLiteDatabase localSQLiteDatabase = this.mHelper.getReadableDatabase();
    Cursor localCursor = null;
    while (true)
    {
      int j;
      try
      {
        localCursor = localSQLiteDatabase.query("DownloadColumns", DC_PROJECTION, null, null, null, null, null);
        DownloadInfo[] arrayOfDownloadInfo;
        if ((localCursor != null) && (localCursor.moveToFirst()))
        {
          arrayOfDownloadInfo = new DownloadInfo[localCursor.getCount()];
          i = 0;
          DownloadInfo localDownloadInfo = getDownloadInfoFromCursor(localCursor);
          j = i + 1;
          arrayOfDownloadInfo[i] = localDownloadInfo;
          boolean bool = localCursor.moveToNext();
          if (!bool)
            return arrayOfDownloadInfo;
        }
        else
        {
          arrayOfDownloadInfo = null;
          return null;
        }
      }
      finally
      {
        if (localCursor != null)
          localCursor.close();
      }
      int i = j;
    }
  }

  public int getFlags()
  {
    return this.mFlags;
  }

  public long getIDByIndex(int paramInt)
  {
    SQLiteStatement localSQLiteStatement = getDownloadByIndexStatement();
    localSQLiteStatement.clearBindings();
    localSQLiteStatement.bindLong(1, paramInt);
    try
    {
      long l = localSQLiteStatement.simpleQueryForLong();
      return l;
    }
    catch (SQLiteDoneException localSQLiteDoneException)
    {
    }
    return -1L;
  }

  public long getIDForDownloadInfo(DownloadInfo paramDownloadInfo)
  {
    return getIDByIndex(paramDownloadInfo.mIndex);
  }

  public int getLastCheckedVersionCode()
  {
    return this.mVersionCode;
  }

  public boolean isDownloadRequired()
  {
    boolean bool = true;
    Cursor localCursor = this.mHelper.getReadableDatabase().rawQuery("SELECT Count(*) FROM DownloadColumns WHERE STATUS <> 0", null);
    if (localCursor != null);
    try
    {
      if (localCursor.moveToFirst())
      {
        int i = localCursor.getInt(0);
        if (i == 0);
        while (true)
        {
          return bool;
          bool = false;
        }
      }
      return bool;
    }
    finally
    {
      if (localCursor != null)
        localCursor.close();
    }
  }

  public void setDownloadInfoFromCursor(DownloadInfo paramDownloadInfo, Cursor paramCursor)
  {
    paramDownloadInfo.mUri = paramCursor.getString(1);
    paramDownloadInfo.mETag = paramCursor.getString(2);
    paramDownloadInfo.mTotalBytes = paramCursor.getLong(3);
    paramDownloadInfo.mCurrentBytes = paramCursor.getLong(4);
    paramDownloadInfo.mLastMod = paramCursor.getLong(5);
    paramDownloadInfo.mStatus = paramCursor.getInt(6);
    paramDownloadInfo.mControl = paramCursor.getInt(7);
    paramDownloadInfo.mNumFailed = paramCursor.getInt(8);
    paramDownloadInfo.mRetryAfter = paramCursor.getInt(9);
    paramDownloadInfo.mRedirectCount = paramCursor.getInt(10);
  }

  public boolean updateDownload(DownloadInfo paramDownloadInfo)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("FILEIDX", Integer.valueOf(paramDownloadInfo.mIndex));
    localContentValues.put("FN", paramDownloadInfo.mFileName);
    localContentValues.put("URI", paramDownloadInfo.mUri);
    localContentValues.put("ETAG", paramDownloadInfo.mETag);
    localContentValues.put("TOTALBYTES", Long.valueOf(paramDownloadInfo.mTotalBytes));
    localContentValues.put("CURRENTBYTES", Long.valueOf(paramDownloadInfo.mCurrentBytes));
    localContentValues.put("LASTMOD", Long.valueOf(paramDownloadInfo.mLastMod));
    localContentValues.put("STATUS", Integer.valueOf(paramDownloadInfo.mStatus));
    localContentValues.put("CONTROL", Integer.valueOf(paramDownloadInfo.mControl));
    localContentValues.put("FAILCOUNT", Integer.valueOf(paramDownloadInfo.mNumFailed));
    localContentValues.put("RETRYAFTER", Integer.valueOf(paramDownloadInfo.mRetryAfter));
    localContentValues.put("REDIRECTCOUNT", Integer.valueOf(paramDownloadInfo.mRedirectCount));
    return updateDownload(paramDownloadInfo, localContentValues);
  }

  public boolean updateDownload(DownloadInfo paramDownloadInfo, ContentValues paramContentValues)
  {
    long l1;
    if (paramDownloadInfo == null)
      l1 = -1L;
    try
    {
      SQLiteDatabase localSQLiteDatabase;
      while (true)
      {
        localSQLiteDatabase = this.mHelper.getWritableDatabase();
        if (l1 == -1L)
          break;
        int i = localSQLiteDatabase.update("DownloadColumns", paramContentValues, "DownloadColumns._id = " + l1, null);
        if (1 != i);
        return false;
        l1 = getIDForDownloadInfo(paramDownloadInfo);
      }
      long l2 = localSQLiteDatabase.insert("DownloadColumns", "URI", paramContentValues);
      if (-1L != l2);
      for (boolean bool = true; ; bool = false)
        return bool;
    }
    catch (SQLiteException localSQLiteException)
    {
      localSQLiteException.printStackTrace();
    }
    return false;
  }

  public void updateDownloadCurrentBytes(DownloadInfo paramDownloadInfo)
  {
    SQLiteStatement localSQLiteStatement = getUpdateCurrentBytesStatement();
    localSQLiteStatement.clearBindings();
    localSQLiteStatement.bindLong(1, paramDownloadInfo.mCurrentBytes);
    localSQLiteStatement.bindLong(2, paramDownloadInfo.mIndex);
    localSQLiteStatement.execute();
  }

  public boolean updateFlags(int paramInt)
  {
    if (this.mFlags != paramInt)
    {
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("DOWNLOADFLAGS", Integer.valueOf(paramInt));
      if (updateMetadata(localContentValues))
        this.mFlags = paramInt;
    }
    else
    {
      return true;
    }
    return false;
  }

  public boolean updateFromDb(DownloadInfo paramDownloadInfo)
  {
    SQLiteDatabase localSQLiteDatabase = this.mHelper.getReadableDatabase();
    Cursor localCursor = null;
    try
    {
      String[] arrayOfString1 = DC_PROJECTION;
      String[] arrayOfString2 = new String[1];
      arrayOfString2[0] = paramDownloadInfo.mFileName;
      localCursor = localSQLiteDatabase.query("DownloadColumns", arrayOfString1, "FN= ?", arrayOfString2, null, null, null);
      if ((localCursor != null) && (localCursor.moveToFirst()))
      {
        setDownloadInfoFromCursor(paramDownloadInfo, localCursor);
        return true;
      }
      return false;
    }
    finally
    {
      if (localCursor != null)
        localCursor.close();
    }
  }

  public boolean updateMetadata(int paramInt1, int paramInt2)
  {
    ContentValues localContentValues = new ContentValues();
    localContentValues.put("APKVERSION", Integer.valueOf(paramInt1));
    localContentValues.put("DOWNLOADSTATUS", Integer.valueOf(paramInt2));
    if (updateMetadata(localContentValues))
    {
      this.mVersionCode = paramInt1;
      this.mStatus = paramInt2;
      return true;
    }
    return false;
  }

  public boolean updateMetadata(ContentValues paramContentValues)
  {
    SQLiteDatabase localSQLiteDatabase = this.mHelper.getWritableDatabase();
    if (-1L == this.mMetadataRowID)
    {
      l = localSQLiteDatabase.insert("MetadataColumns", "APKVERSION", paramContentValues);
      if (-1L == l)
        return false;
      this.mMetadataRowID = l;
    }
    while (localSQLiteDatabase.update("MetadataColumns", paramContentValues, "_id = " + this.mMetadataRowID, null) != 0)
    {
      long l;
      return true;
    }
    return false;
  }

  public boolean updateStatus(int paramInt)
  {
    if (this.mStatus != paramInt)
    {
      ContentValues localContentValues = new ContentValues();
      localContentValues.put("DOWNLOADSTATUS", Integer.valueOf(paramInt));
      if (updateMetadata(localContentValues))
        this.mStatus = paramInt;
    }
    else
    {
      return true;
    }
    return false;
  }

  public static class DownloadColumns
    implements BaseColumns
  {
    public static final String CONTROL = "CONTROL";
    public static final String CURRENTBYTES = "CURRENTBYTES";
    public static final String ETAG = "ETAG";
    public static final String FILENAME = "FN";
    public static final String INDEX = "FILEIDX";
    public static final String LASTMOD = "LASTMOD";
    public static final String NUM_FAILED = "FAILCOUNT";
    public static final String REDIRECT_COUNT = "REDIRECTCOUNT";
    public static final String RETRY_AFTER = "RETRYAFTER";
    public static final String[][] SCHEMA = { { "_id", "INTEGER PRIMARY KEY" }, { "FILEIDX", "INTEGER UNIQUE" }, { "URI", "TEXT" }, { "FN", "TEXT UNIQUE" }, { "ETAG", "TEXT" }, { "TOTALBYTES", "INTEGER" }, { "CURRENTBYTES", "INTEGER" }, { "LASTMOD", "INTEGER" }, { "STATUS", "INTEGER" }, { "CONTROL", "INTEGER" }, { "FAILCOUNT", "INTEGER" }, { "RETRYAFTER", "INTEGER" }, { "REDIRECTCOUNT", "INTEGER" } };
    public static final String STATUS = "STATUS";
    public static final String TABLE_NAME = "DownloadColumns";
    public static final String TOTALBYTES = "TOTALBYTES";
    public static final String URI = "URI";
    public static final String _ID = "DownloadColumns._id";
  }

  protected static class DownloadsContentDBHelper extends SQLiteOpenHelper
  {
    private static final String[][][] sSchemas = arrayOfString;;
    private static final String[] sTables = { "DownloadColumns", "MetadataColumns" };

    static
    {
      String[][][] arrayOfString; = new String[2][][];
      arrayOfString;[0] = DownloadsDB.DownloadColumns.SCHEMA;
      arrayOfString;[1] = DownloadsDB.MetadataColumns.SCHEMA;
    }

    DownloadsContentDBHelper(Context paramContext)
    {
      super("DownloadsDB", null, 7);
    }

    private String createTableQueryFromArray(String paramString, String[][] paramArrayOfString)
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("CREATE TABLE ");
      localStringBuilder.append(paramString);
      localStringBuilder.append(" (");
      int i = paramArrayOfString.length;
      for (int j = 0; ; j++)
      {
        if (j >= i)
        {
          localStringBuilder.setLength(-1 + localStringBuilder.length());
          localStringBuilder.append(");");
          return localStringBuilder.toString();
        }
        String[] arrayOfString = paramArrayOfString[j];
        localStringBuilder.append(' ');
        localStringBuilder.append(arrayOfString[0]);
        localStringBuilder.append(' ');
        localStringBuilder.append(arrayOfString[1]);
        localStringBuilder.append(',');
      }
    }

    private void dropTables(SQLiteDatabase paramSQLiteDatabase)
    {
      String[] arrayOfString = sTables;
      int i = arrayOfString.length;
      int j = 0;
      while (true)
        if (j < i)
        {
          String str = arrayOfString[j];
          try
          {
            paramSQLiteDatabase.execSQL("DROP TABLE IF EXISTS " + str);
            j++;
          }
          catch (Exception localException)
          {
            while (true)
              localException.printStackTrace();
          }
        }
    }

    public void onCreate(SQLiteDatabase paramSQLiteDatabase)
    {
      int i = sSchemas.length;
      int j = 0;
      while (true)
        if (j < i)
          try
          {
            String[][] arrayOfString = (String[][])sSchemas[j];
            paramSQLiteDatabase.execSQL(createTableQueryFromArray(sTables[j], arrayOfString));
            j++;
          }
          catch (Exception localException)
          {
            while (true)
              localException.printStackTrace();
          }
    }

    public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2)
    {
      Log.w(DownloadsContentDBHelper.class.getName(), "Upgrading database from version " + paramInt1 + " to " + paramInt2 + ", which will destroy all old data");
      dropTables(paramSQLiteDatabase);
      onCreate(paramSQLiteDatabase);
    }
  }

  public static class MetadataColumns
    implements BaseColumns
  {
    public static final String APKVERSION = "APKVERSION";
    public static final String DOWNLOAD_STATUS = "DOWNLOADSTATUS";
    public static final String FLAGS = "DOWNLOADFLAGS";
    public static final String[][] SCHEMA = { { "_id", "INTEGER PRIMARY KEY" }, { "APKVERSION", "INTEGER" }, { "DOWNLOADSTATUS", "INTEGER" }, { "DOWNLOADFLAGS", "INTEGER" } };
    public static final String TABLE_NAME = "MetadataColumns";
    public static final String _ID = "MetadataColumns._id";
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.google.android.vending.expansion.downloader.impl.DownloadsDB
 * JD-Core Version:    0.6.2
 */